package com.zucc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.zucc.controller.req.UserAddReq;
import com.zucc.controller.req.UserLoginReq;
import com.zucc.controller.req.UserModifyReq;
import com.zucc.controller.req.UserSearchReq;
import com.zucc.controller.resp.UserResp;
import com.zucc.entity.User;
import com.zucc.entity.UserMapper;
import com.zucc.entity.base.MyMapper;
import com.zucc.exception.SyzxException;
import com.zucc.service.UserService;
import com.zucc.service.base.MyServiceImpl;
import com.zucc.service.base.PageData;
import com.zucc.util.MyPageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author : charspan
 * @Description :
 * @create : 2021-12-05 8:24 上午
 */
@Service
public class UserServiceImpl extends MyServiceImpl<User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public MyMapper<User> mapper() {
        return userMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(UserAddReq req) {
        Example exp = new Example(User.class);
        exp.createCriteria().andEqualTo("delFlag",0).andEqualTo("account", req.getAccount());
        User one = super.findOne(exp);
        if (Objects.nonNull(one)) {
            throw new SyzxException("此账号已被创建");
        }
        User user = new User();
        BeanUtil.copyProperties(req, user);
        user.setDelFlag(0);
        user.setCreateTime(DateUtil.date());
        user.setModifyTime(DateUtil.date());
        super.saveKey(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modify(UserModifyReq req) {
        User userExist = super.findById(req.getId());
        if (Objects.isNull(userExist) || userExist.getDelFlag().intValue() == 1) {
            throw new SyzxException("系统找不到相应编号的用户信息");
        }
        BeanUtil.copyProperties(req, userExist, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        userExist.setModifyTime(DateUtil.date());
        super.updateSelective(userExist);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOne(Long id) {
        User userExist = super.findById(id);
        if (Objects.isNull(userExist) || userExist.getDelFlag().intValue() == 1) {
            throw new SyzxException("系统找不到相应编号的用户信息");
        }
        userExist.setDelFlag(1);
        userExist.setDeleteTime(DateUtil.date());
        super.updateSelective(userExist);
    }

    @Override
    public PageData<UserResp> page(UserSearchReq req) {
        Example example = new Example(User.class);
        example.setOrderByClause("modify_time DESC");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("delFlag", 0);

        if (StrUtil.isNotBlank(req.getRemarks())) {
            criteria.andLike("remarks", "%" + req.getRemarks() + "%");
        }

        if (StrUtil.isNotBlank(req.getAccount())) {
            criteria.andLike("account", "%" + req.getAccount() + "%");
        }

        if (StrUtil.isNotBlank(req.getBrcachMame())) {
            criteria.andLike("brcachMame", "%" + req.getBrcachMame() + "%");
        }

        if (StrUtil.isNotBlank(req.getTeacherCode())) {
            criteria.andLike("teacherCode", "%" + req.getTeacherCode() + "%");
        }

        if (StrUtil.isNotBlank(req.getTitle())) {
            criteria.andLike("title", "%" + req.getRemarks() + "%");
        }

        if (StrUtil.isNotBlank(req.getClassName())) {
            criteria.andLike("className", "%" + req.getClassName() + "%");
        }

        if (StrUtil.isNotBlank(req.getName())) {
            criteria.andLike("name", "%" + req.getName() + "%");
        }

        if (Objects.nonNull(req.getSex())) {
            criteria.andEqualTo("sex", req.getSex());
        }

        if (StrUtil.isNotBlank(req.getNickname())) {
            criteria.andLike("nickname", "%" + req.getNickname() + "%");
        }

        // 出生日期
        if (Objects.nonNull(req.getBirthday())) {
            criteria.andEqualTo("birthday", req.getBirthday());
        }

        if (StrUtil.isNotBlank(req.getOfficePhone())) {
            criteria.andLike("officePhone", "%" + req.getOfficePhone() + "%");
        }

        if (StrUtil.isNotBlank(req.getMobile())) {
            criteria.andLike("mobile", "%" + req.getMobile() + "%");
        }

        if (StrUtil.isNotBlank(req.getEmail())) {
            criteria.andLike("email", "%" + req.getEmail() + "%");
        }

        if (Objects.nonNull(req.getRoleType())) {
            criteria.andEqualTo("roleType", req.getRoleType());
        }

        PageData<User> pageData = super.findByPage(req, example);
        PageData<UserResp> convert = MyPageUtil.convert(pageData, UserResp.class);
        return convert;
    }

    @Override
    public UserResp login(UserLoginReq req) {
        Example exp = new Example(User.class);
        exp.createCriteria().andEqualTo("delFlag", 0)
                .andEqualTo("account", req.getAccount())
                .andEqualTo("password", req.getPassword());
        User one = super.findOne(exp);
        if (Objects.isNull(one)) {
            throw new SyzxException("账号和密码不匹配");
        }
        UserResp userResp = new UserResp();
        BeanUtil.copyProperties(one, userResp);
        return userResp;
    }
}
